﻿using DAO.BLL;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class TempletController : Controller
    {

        //单元测试
        //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

        public ActionResult Index()
        {

            return View();
        }

        public new ActionResult User(int pageIndex = 1, int pageSize = 10)
        {
            B_Users b_user = new B_Users();
            var list_user = b_user.GetList(pageIndex, pageSize,null,"","");
            return View();
        }

        public ActionResult Authority()
        {

            return View();
        }

        public ActionResult Navigation()
        {
            return View();
        }

        public ActionResult Manager_role() 
        {
            //获取所有的后台导航
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };

            List<Domain.Navigation> list = new List<Domain.Navigation>();
            var list_nav = b_nav.LoadAll(order, 0);
            foreach (var nav in list_nav)
            {
                list.Add(nav);
                //查询二级菜单
                var list_sub_nav = b_nav.LoadAll(order, nav.id);
                foreach (var sub_nav in list_sub_nav)
                {
                    list.Add(sub_nav);
                }
            }
            ViewBag.list_nav = list;
            return View();
        }



        #region User

        [HttpPost]
        public JsonResult GetUserList(int limit = 10, int offset = 1, string user_name = "", string mobile = "")
        {
            B_Users b_user = new B_Users();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            var list_user = b_user.GetList(offset, limit, order, user_name, mobile);

            var total = b_user.GetCount(user_name, mobile);

            return Json(new { total = total, rows = list_user }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AddUser(string txt_user_name,string txt_mobile,string txt_email,string txt_nick_name,string txt_password) 
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            var res = b_user.GetCount(user_name:txt_user_name);
            if (res > 0) 
            {
                json.status = -1;
                json.msg = "用户名已存在!";
                json.pitchId = "txt_user_name";
                return Json(json);
            }
            res = b_user.GetCount(mobile:txt_mobile);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "手机号已存在!";
                json.pitchId = "txt_mobile";
                return Json(json);
            }
            Domain.Users m_user = new Domain.Users();
            m_user.user_name = txt_user_name;
            m_user.mobile = txt_mobile;
            m_user.password =  Common.Encrypt.md5(txt_password);
            m_user.email = txt_email;
            m_user.nick_name = txt_nick_name;
            res = b_user.Save(m_user);
            if (res <= 0) 
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";
            return Json(json);
        }

        [HttpPost]
        public JsonResult GetUser(int id) 
        {
            DAO.BLL.B_Users b_user = new B_Users();
            var res = b_user.GetUser(id);
            return Json(res);
        }

        [HttpPost]
        public JsonResult EditUser(int id,string txt_user_name, string txt_mobile, string txt_email, string txt_nick_name, string txt_password) 
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            var m_user = b_user.GetUser(id);
            if (m_user.user_name != txt_user_name) 
            {
                var res = b_user.GetCount(user_name:txt_user_name);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "用户名已存在!";
                    json.pitchId = "txt_user_name";
                    return Json(json);
                }
            }
            if (m_user.mobile != txt_mobile) 
            {
                var res = b_user.GetCount(mobile: txt_mobile);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "手机号已存在!";
                    json.pitchId = "txt_mobile";
                    return Json(json);
                }
            }
            m_user.user_name = txt_user_name;
            m_user.mobile = txt_mobile;
            if (m_user.password != txt_password)
            {
                m_user.password = Common.Encrypt.md5(txt_password);
            }
            m_user.email = txt_email;
            m_user.nick_name = txt_nick_name;
            b_user.Update(m_user);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        public JsonResult DelUser(string ids) 
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            foreach (var id in ids.Split(new char[]{','}))
            {
                b_user.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Navigation
        [HttpPost]
        public JsonResult GetNavList(int limit = 10, int offset = 1)
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };

            List<Domain.Navigation> list = new List<Domain.Navigation>();
            var list_nav = b_nav.GetList(offset,limit,order, 0);
            foreach (var nav in list_nav)
            {
                list.Add(nav);
                //查询二级菜单
                var list_sub_nav = b_nav.GetList(offset, limit, order, nav.id);
                foreach (var sub_nav in list_sub_nav)
                {
                    list.Add(sub_nav);
                }
            }
            return Json(new { total = list.Count, rows = list }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetNavSubList(string index, string parent_id,int limit =10, int offset = 1) 
        {
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };
            var list_nav = b_nav.GetList(offset,limit,order,Convert.ToInt32(parent_id));
            return Json(list_nav, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult AddNav(string txt_parent_id, string txt_icon_url, string txt_title, string txt_link_url, string txt_sort_id, string txt_is_lock, string txt_action_type) 
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            Domain.Navigation model = new Domain.Navigation();

            model.icon_url = txt_icon_url;
            model.title = txt_title;
            model.link_url = txt_link_url;
            model.sort_id = Convert.ToInt32(txt_sort_id);
            model.is_lock = txt_is_lock;
            model.parent_id = Convert.ToInt32(txt_parent_id);
            model.action_type = txt_action_type;
            if (model.parent_id == 0)
            {
                model.channel_id = 1;
            }
            else 
            {
                model.channel_id = 2;
            }
            var res = b_nav.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";

            return Json(json);
           
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult EditNav(int id, string txt_parent_id, string txt_icon_url, string txt_title, string txt_link_url, string txt_sort_id, string txt_is_lock, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            Domain.Navigation model = b_nav.GetNav(id);
            model.icon_url = txt_icon_url;
            model.title = txt_title;
            model.link_url = txt_link_url;
            model.sort_id = Convert.ToInt32(txt_sort_id);
            model.is_lock = txt_is_lock;
            model.parent_id = Convert.ToInt32(txt_parent_id);
            model.action_type = txt_action_type;
            if (model.parent_id == 0)
            {
                model.channel_id = 1;
            }
            else
            {
                model.channel_id = 2;
            }
            b_nav.Update(model);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        public JsonResult GetNav(int id) 
        {
            B_Navigation b_nav = new B_Navigation();
            var model = b_nav.GetNav(id);
            return Json(model);
        }

        [HttpPost]
        public JsonResult DelNav(string ids)
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_nav.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Manager_role
        [HttpPost]
        public JsonResult GetManager_roleList(int limit = 10, int offset = 1)
        {
            B_Manager_role b_mr = new B_Manager_role();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            var list_user = b_mr.GetList(offset, limit, order);

            var total = b_mr.GetCount();

            return Json(new { total = total, rows = list_user }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AddManager_role(string txt_role_name, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Manager_role b_mr = new DAO.BLL.B_Manager_role();
            var res = b_mr.GetCount(txt_role_name);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "角色名已存在!";
                json.pitchId = "txt_role_name";
                return Json(json);
            }
            Domain.Manager_role model = new Domain.Manager_role();
            model.role_name = txt_role_name;
            res = b_mr.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            b_mrv.Update(txt_action_type, res);
                json.msg = "添加成功!";
            
            return Json(json);
        }

        [HttpPost]
        public JsonResult CheckManager_role_value(int role_id,int nav_id,string action_type) 
        {
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            var list = b_mrv.GetList(role_id, nav_id);
            if (list.Count == 0)
            {
                return Json("false");
            }
            else 
            {
                if (list[0].action_type.Contains(action_type))
                {
                    return Json("true");
                }
                else 
                {
                    return Json("false");
                }
            }
        }

        [HttpPost]
        public JsonResult GetManager_role(int id) 
        {
            B_Manager_role b_mr = new B_Manager_role();
            var res = b_mr.Get(id);
            return Json(res);
        }

        [HttpPost]
        public JsonResult EditManager_role(int id,string txt_role_name, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Manager_role b_mr = new DAO.BLL.B_Manager_role();
            var m_mr = b_mr.Get(id);
            var res = b_mr.GetCount(txt_role_name);
            if (m_mr.role_name != txt_role_name)
            {
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "角色名已存在!";
                    json.pitchId = "txt_role_name";
                    return Json(json);
                }
            }
            Domain.Manager_role model = m_mr;
            model.role_name = txt_role_name;
            b_mr.Update(model);
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            b_mrv.Update(txt_action_type, model.id);
            json.msg = "修改成功!";

            return Json(json);
        }

        [HttpPost]
        public JsonResult DelManager_role(string ids)
        {
            Common.Json json = new Common.Json();
            B_Manager_role b_mr = new B_Manager_role();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_mr.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }

        #endregion
    }
}
